package com.zkingsoft.web.tool;

import com.zkingsoft.common.bean.SysCompany;
import com.zkingsoft.common.service.SysCompanyService;
import com.zkingsoft.web.dao.ProductAttrDefDao;
import com.zkingsoft.web.dao.ProductAttrRefDao;
import com.zkingsoft.web.dao.ProductDao;
import com.zkingsoft.web.service.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description: 初始化web容器的类 一些网站启动需要加载的数据和方法在这里配置和执行
 * @author:姜友瑶
 * @date 2016年10月18日
 */
@Controller
public class WebInitContainer implements ServletContextAware {

	private ServletContext servletContext;

	/**
	 * 是否为debug模式
	 */
	@Value("${debug}")
	private String debug;

	@Value("${nginx_url}")
	private String nginx_url;

	@Value("${file_storage_path}")
	private String file_storage_path;

	@Resource(name = "busParameterSettingsService")
	private BusParameterSettingsService busParameterSettingsService;

	@Resource(name = "webNavService")
	private WebNavService webNavService;

	@Resource(name = "webAdService")
	private WebAdService webAdService;
	
	@Resource(name = "webAdcolumnService")
	private WebAdcolumnService webAdcolumnService;

	@Resource(name = "webArticleService")
	private WebArticleService artService;

	@Resource(name = "webArticleTypeService")
	private WebArticleTypeService artTypeService;
	
	@Resource(name = "productDao")
	private ProductDao productDao;
	
	@Resource(name = "productAttrDefDao")
	private ProductAttrDefDao productAttrDefDao;
	
	@Resource(name = "productAttrRefDao")
	private ProductAttrRefDao productAttrRefDao;
	
	
	
	@Autowired
	private SysCompanyService sysCompanyService;
	
	

	// 定义打印log
	Logger log = Logger.getLogger(this.getClass());

	/**
	 * 
	 * @Description: 容器启动后加载数据
	 * @author:姜友瑶
	 * @param sc
	 * @date 2016年10月18日
	 */
	public void setServletContext(ServletContext sc) {
		this.servletContext = sc;
		log.info("web 项目启动***********************" + "项目的绝对路径为：" + servletContext.getRealPath("/"));
		log.info("debug=" + debug);
		if ("true".equals(debug)) {
			SystemConstance.DEBUG = true;
		} else {
			SystemConstance.DEBUG = false;
		}
		log.info("SystemConstance.DEBUG=" + SystemConstance.DEBUG);
		servletContext.setAttribute(SystemConstance.NGINX_URL, nginx_url);
		servletContext.setAttribute(SystemConstance.FILE_STORAGE_PATH, file_storage_path);
		initParams();
		saveService();
	}

	/**
	 * 把需要用到的service存在web容器中
	 * 
	 * @param sc
	 */
	private void saveService() {
		servletContext.setAttribute(SystemConstance.NAV_SERVICE, webNavService);
		servletContext.setAttribute(SystemConstance.AD_SERVICE, webAdService);
		servletContext.setAttribute(SystemConstance.ADCOLUMN_SERVICE, webAdcolumnService);
		servletContext.setAttribute(SystemConstance.ART_SERVICE, artService);
		servletContext.setAttribute(SystemConstance.ARTTYPE_SERVICE, artTypeService);
		servletContext.setAttribute(SystemConstance.PRODUCTDAO, productDao);
		servletContext.setAttribute(SystemConstance.PRODUCT_ATTRDEFDAO, productAttrDefDao);
		servletContext.setAttribute(SystemConstance.PRODUCT_ATTRREFDAO, productAttrRefDao);
	}

	/**
	 * 初始化网站的关键词和描述
	 * 
	 * @param sc
	 */
	public  void initParams() {
		
		//初始化所有的公司
		List<SysCompany> company=sysCompanyService.findByModel(null);
		Map<String, SysCompany> companyMap=new HashMap<>();
		for (SysCompany sysCompany : company) {
			companyMap.put(sysCompany.getComWebUrl(), sysCompany);
		}
		servletContext.setAttribute("companyMap", companyMap);
	}
 

}
